package com.song.user.service.impl;

import com.song.user.dao.UserDao;
import com.song.user.pojo.User;
import com.song.user.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 宋伟宁
 * @Date 2024/1/5
 * @Version 1.0
 **/
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;
    @Override
    public List<User> findAll() {
        return this.userDao.findAll();
    }

    @Override
    public User findById(Long id) {
        return this.userDao.findById(id).get();
    }

    @Override
    public boolean save(User user) {
        try {
            this.userDao.save(user);
            return true;
        }catch (Exception e){
            log.debug("新增失败，用户名重复或其它。。。。"+e.getMessage());
        }
        return false;
    }

    @Override
    public boolean delete(Long id) {
        try {
            this.userDao.deleteById(id);
            return true;
        }catch (Exception e){
            log.debug(e.getMessage());
        }
        return false;
    }

    @Override
    public int findUserByUsername(String username) {
        return this.userDao.countByUsername(username);
    }

    @Override
    public User login(String username, String password) {
        return this.userDao.findUserByUsernameAndPassword(username, password);
    }
}
